﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AudoCutApp
{
    /// <summary>
    /// RactangControl.xaml 的交互逻辑
    /// </summary>
    public partial class RactangControl : UserControl
    {
        public RactangControl()
        {
            InitializeComponent();
        }
         
        public bool IsLeftDrow { get; set; } = false;
        public bool IsRightDrow { get; set; } = false;




        public Point StartPoint
        {
            get { return (Point)GetValue(StartPointProperty); }
            set { SetValue(StartPointProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StartPoint.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StartPointProperty =
            DependencyProperty.Register("StartPoint", typeof(Point), typeof(RactangControl), new PropertyMetadata(new Point(0, 0), OnStartChanged));

        private static void OnStartChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctrl = (RactangControl)d;
            if (ctrl.EndPoint.X > ctrl.StartPoint.X)
            {
                ctrl.start.Text = ctrl.StartPoint.X.ToString("N0");

                ctrl.end.Text = ctrl.EndPoint.X.ToString("N0");
                ctrl.Width = ctrl.EndPoint.X - ctrl.StartPoint.X;
            }
        }




        public Point EndPoint
        {
            get { return (Point)GetValue(EndPointProperty); }
            set { SetValue(EndPointProperty, value); }
        }

        // Using a DependencyProperty as the backing store for EndPoint.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EndPointProperty =
            DependencyProperty.Register("EndPoint", typeof(Point), typeof(RactangControl), new PropertyMetadata(new Point(0, 0), OnStartChanged));



        public void MouseDownMove(MouseEventArgs e)
        {
            start.Text = StartPoint.X.ToString("N0");
            end.Text = EndPoint.X.ToString("N0");
            if (IsLeftDrow)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    var point = e.GetPosition((FrameworkElement)this.Parent);
                    Console.WriteLine($"point:{point.X}-Start:{StartPoint.X}-end:{EndPoint.X}");
                    StartPoint = point;
                    this.Width = EndPoint.X - point.X;
                    Canvas.SetLeft(this, point.X);
                }
            }
            if (IsRightDrow)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    var point = e.GetPosition((FrameworkElement)this.Parent);
                    EndPoint = point;
                    this.Width = EndPoint.X - StartPoint.X;
                }
            }
        }


        private void MouseMoveLeft_Click(object sender, MouseEventArgs e)
        {
            var border = (Border)sender;

            //Console.WriteLine($"{IsDrawing}--{e.LeftButton.ToString()}-Start:{StartPoint.X}-end:{EndPoint.X}");
            //if (e.LeftButton == MouseButtonState.Pressed&&!IsDrawing)
            //{
            //    var point=e.GetPosition((FrameworkElement)this.Parent);
            //    Console.WriteLine($"point:{point.X}-Start:{StartPoint.X}-end:{EndPoint.X}");
            //    StartPoint = point;
            //    this.Width = EndPoint.X - point.X;
            //    Canvas.SetLeft(this,point.X);
            //}
        }

        private void MouseMoveRight_Click(object sender, MouseEventArgs e)
        {
            //Console.WriteLine($"Right:-{IsDrawing}--{e.LeftButton.ToString()}-Start:{StartPoint.X}-end:{EndPoint.X}");
            //if (e.LeftButton == MouseButtonState.Pressed && !IsDrawing)
            //{
            //    var point = e.GetPosition((FrameworkElement)this.Parent);
            //   // Console.WriteLine($"point:{point.X}-Start:{StartPoint.X}-end:{EndPoint.X}");
            //    EndPoint = point;
            //    this.Width = EndPoint.X - StartPoint.X;  
            //}
        }

        private void MousLeftDown_Click(object sender, MouseButtonEventArgs e)
        {
            IsLeftDrow = true;
        }

        private void MousLeftUp_Click(object sender, MouseButtonEventArgs e)
        {
            IsLeftDrow = false;
        }

        private void MousRightDown_Click(object sender, MouseButtonEventArgs e)
        {
            IsRightDrow = true;
        }

        private void MousRightUp_Click(object sender, MouseButtonEventArgs e)
        {
            IsRightDrow = false;
        }
    }
}
